package com.zzw.service;

import com.zzw.entity.SysRole;
import com.zzw.entity.SysUser;
import com.zzw.mapper.dao.SysRoleMapper;
import com.zzw.mapper.dao.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Optional;
import java.util.Set;

@Service
@CacheConfig(cacheNames = "sysUserService")
public class SysUserService  {
    @Autowired
    private SysUserMapper sysUserMapper;

    @CachePut(key = "#p0.id+':'", unless = "#result eq null")  //#p0表示第一个参数
    public SysUser insertSysUser(SysUser sysUser) {
        sysUserMapper.insert(sysUser);
        return sysUser;
    }

    @CachePut(key = "#p0.id+':'", unless = "#result eq null")  //#p0表示第一个参数
    public SysUser updateSysUser(SysUser sysUser) {
        sysUserMapper.updateByPrimaryKey(sysUser);
        return sysUser;
    }

    @CacheEvict(key = "#p0.id+':'")  //删除缓存名称为userCache,key等于指定的id对应的缓存
    public int deleteSysUser(SysUser sysUser) {
        return sysUserMapper.delete(sysUser);
    }

    @Cacheable(key = "#p0.id+':'", unless = "#result eq null")  //#p0表示第一个参数
    public SysUser selectSysUser(SysUser sysUser) {
        return sysUserMapper.selectOne(sysUser);
    }

    @CacheEvict(allEntries = true)
    public int deleteAll() {
        return sysUserMapper.delete(null);
    }

    @CacheEvict(allEntries = true)
    public int deleteByIds(String ids) {
        return sysUserMapper.deleteByIds(ids);
    }

}
